package com.czk.autoask.consumer;

import com.czk.utils.RabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 消费者二
 * 默认的情况是轮询的平均消费消息，消费者消费的数量是一致的
 *
 * @Author:ChenZhangKun
 * @Date: 2021/1/2 17:25
 */
public class SecondConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection czkConnection = RabbitmqUtils.getCzkConnection();
        Channel channel = czkConnection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare("work", true, false, false, null);
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者二接收到了 " + new String(body));
                // 确认的envelope.getDeliveryTag()消息标记
                channel.basicAck(envelope.getDeliveryTag(),false);
            }

        });
    }
}
